package com.bdqn.demo01.controller;


import com.bdqn.demo01.entity.Account;
import com.bdqn.demo01.service.AccountService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    // 登录页面
    @GetMapping("/login")
    public String loginPage() {
        return "account/login";
    }

    // 处理登录请求
    @PostMapping("/login")
    public String login(@RequestParam String cardno, @RequestParam String password, HttpSession session, Model model) {
        Optional<Account> accountOptional = accountService.findByCardnoAndPassword(cardno, password);
        if (accountOptional.isPresent()) {
            session.setAttribute("account", accountOptional.get());
            return "redirect:/account/list";
        } else {
            model.addAttribute("error", "卡号或密码错误");
            return "account/login";
        }
    }

    // 查询所有账户
    @GetMapping("/list")
    public String list(Model model, HttpSession session) {
        if (session.getAttribute("account") == null) {
            return "redirect:/account/login";
        }
        List<Account> accounts = accountService.list();
        model.addAttribute("accounts", accounts);
        return "account/list";
    }

    // 跳转到添加页面
    @GetMapping("/add")
    public String addPage(HttpSession session) {
        if (session.getAttribute("account") == null) {
            return "redirect:/account/login";
        }
        return "account/add";
    }

    // 添加账户
    @PostMapping("/add")
    public String add(@ModelAttribute Account account, HttpSession session) {
        if (session.getAttribute("account") == null) {
            return "redirect:/account/login";
        }
        accountService.save(account);
        return "redirect:/account/list";
    }

    // 跳转到编辑页面
    @GetMapping("/edit/{id}")
    public String editPage(@PathVariable Long id, Model model, HttpSession session) {
        if (session.getAttribute("account") == null) {
            return "redirect:/account/login";
        }
        Account account = accountService.getById(id);
        model.addAttribute("account", account);
        return "account/edit";
    }

    // 编辑账户
    @PostMapping("/edit")
    public String edit(@ModelAttribute Account account, HttpSession session) {
        if (session.getAttribute("account") == null) {
            return "redirect:/account/login";
        }
        accountService.updateById(account);
        return "redirect:/account/list";
    }

    // 删除账户
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Long id, HttpSession session) {
        if (session.getAttribute("account") == null) {
            return "redirect:/account/login";
        }
        accountService.removeById(id);
        return "redirect:/account/list";
    }

    // 注销登录
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/account/login";
    }
}
